package com.iad.view.validator
{
    import mx.validators.ValidationResult;
    import mx.validators.Validator;

    public class FieldMatchValidator extends Validator
    {
      // Define Array for the return value of doValidation().
        private var results:Array;

      public function FieldMatchValidator()
      {
        super();
      }

      public var fieldName: String;
      public var confirmationSource: Object;
      public var confirmationProperty: String;

        // Define the doValidation() method.
        override protected function doValidation(value:Object):Array {

            // Call base class doValidation().
        var results:Array = super.doValidation(value.primary);

            if (value.primary != value.confirmation) {
          results.push(new ValidationResult(true, null, "Mismatch",
          fieldName + " does not match " + fieldName + " confirmation"));

            }

            return results;
        }     

      /**
       *  @private
       *  Grabs the data for the confirmation field from its different sources
       *  if its there and bundles it to be processed by the doValidation routine.
       */
      override protected function getValueFromSource():Object
      {
        var value:Object = {};

        value.primary = super.getValueFromSource();

        if (confirmationSource && confirmationProperty)
        {
          value.confirmation = confirmationSource[confirmationProperty];
        }

        return  value;
      }   

    }
}
